/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.util.NumberUtil;
import io.renren.common.constant.CommonConstant;
import io.renren.modules.category.entity.TBrandEntity;
import io.renren.modules.category.entity.TInitialEntity;
import io.renren.modules.category.service.TBrandService;
import io.renren.modules.category.service.TInitialService;
import io.renren.modules.sys.dto.ExcelDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.*;


@RunWith(SpringRunner.class)
@SpringBootTest
public class ExcelTest818 {

    @Autowired
    TBrandService brandService;
    @Autowired
    TInitialService initialService;

    @Test
    public void testLengh(){
        String str="https://mini-app-static.jd.com/apps/mpshare/index.html?category=jump&des=jdmp&vapptype=1&appId=38601A55BA63117CDBE3E6049ACE6EDD&path=pages%2FHome%2FStartVideo.html&scene=001&param=%7B%22utm_medium%22%3A%22display_paid%22%2C%22utm_source%22%3A%22JD_SUPPORT%22%7D\n";
        System.out.printf("str:"+str.length());

    }
    @Test
    public void testImportExcel(){
        String filePath = "D:\\devtools\\work\\奢侈品/brand1222.xlsx";
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(0); // 设置标题行数，默认为0
        importParams.setHeadRows(1); // 设置表头行数，默认为1
        List<ExcelDTO> dataListAll = new ArrayList<>();
        List<ExcelDTO> dataList;
        try {
            dataList = ExcelImportUtil.importExcel(new File(filePath), ExcelDTO.class, importParams);
            dataListAll.addAll(dataList);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        List<String> alphabetList = Arrays.asList(CommonConstant.alphabetArr);
        String alphabet="";
        // 对导入的数据进行处理
        //initial数据map,key=initialEntity.getCode() ；值为表自增ID,品牌表存值需要
        Map<String, Long> initialMap = new HashMap<>();
        List<TBrandEntity> listBrandEntity = new ArrayList<>();
        for(String alphabetstr: alphabetList){
            Map<String, Object> initialParams = new HashMap<>();
            initialParams.put("code",alphabetstr);
            List<TInitialEntity> initialEntities = initialService.selectList(initialParams);
            for(TInitialEntity initialEntity : initialEntities){
                initialMap.put(initialEntity.getCode(),initialEntity.getId());
            }
        }
        for (ExcelDTO data : dataListAll) {
            //如果只有一个字符，则认为是品牌首字母
            if (Objects.isNull(data.getBrandName())){
                continue;
            }
            if (1 == data.getBrandName().length()){
                alphabet =data.getBrandName();
                continue;
            }
            // 处理每一行数据
            TBrandEntity brandEntity = new TBrandEntity();
            brandEntity.setName(data.getBrandName());
            boolean isInteger = NumberUtil.isInteger(data.getSort());
            brandEntity.setSort(isInteger ? Integer.valueOf(data.getSort()) : 0);
            brandEntity.setShopId(data.getShopId());
            brandEntity.setInitialCode(alphabet);
            //根据首字母获取ID
            brandEntity.setInitialId(initialMap.get(alphabet));
            brandEntity.setUrl(data.getUrl());
            brandEntity.setInitialId(initialMap.get(alphabet));
            listBrandEntity.add(brandEntity);
        }
        System.out.println("=====================");
        System.out.println("listBrandEntity==="+listBrandEntity.size());

        boolean insertRes = brandService.insertBatch(listBrandEntity);
        System.out.println("insertFlag==="+insertRes);

    }



}
